package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.domain.CompanyInfo;
import com.sanjishu.enterlab.electricity.sale.domain.DetailInfo;
import com.sanjishu.enterlab.electricity.sale.mapper.CompanyInfoMapper;
import com.sanjishu.enterlab.electricity.sale.mapper.DetailInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DetailInfoService {

    @Autowired
    private DetailInfoMapper detailInfoMapper;
    @Autowired
    private CompanyInfoMapper companyInfoMapper;

    public DetailInfo createDetailInfo(Integer companyId, String voltageStep, String accountArr,
                                       String transformerCapacity, String elecConsumption,
                                       Integer cityId, String detailCategory, String bankName, String bankAccountName,
                                       String bankAccount, String contactPhone, String fax) {
        DetailInfo detailInfo = new DetailInfo();
        CompanyInfo companyInfo = companyInfoMapper.selectByPrimaryKey(companyId);
        if (cityId != null) {
            companyInfo.setCityId(cityId);
            companyInfoMapper.updateByPrimaryKey(companyInfo);
        }
        detailInfo.setCompanyInfoId(companyId);
        detailInfo.setName(companyInfo.getName());
        detailInfo.setElecAccountNum(accountArr);
        detailInfo.setBankName(bankName);
        detailInfo.setBankAccount(bankAccount);
        detailInfo.setBankAccountName(bankAccountName);
        detailInfo.setVoltageStep(voltageStep);
        detailInfo.setTransformerCapacity(transformerCapacity);
        detailInfo.setElecConsumption(elecConsumption);
        detailInfo.setContactPhone(contactPhone);
        detailInfo.setDetailCategory(detailCategory);
        detailInfo.setFax(fax);
        detailInfo.setCreateAt(new Date());
        detailInfoMapper.insert(detailInfo);
        return detailInfo;
    }

    public DetailInfo updateDetailInfo(Integer id, Integer companyId, String voltageStep, String accountArr,
                                       String transformerCapacity, String elecConsumption,
                                       Integer cityId, String detailCategory, String bankName, String bankAccountName,
                                       String bankAccount, String contactPhone, String fax) {
        DetailInfo detailInfo = detailInfoMapper.selectByPrimaryKey(id);
        CompanyInfo companyInfo = companyInfoMapper.selectByPrimaryKey(companyId);
        if (cityId != null) {
            companyInfo.setCityId(cityId);
            companyInfoMapper.updateByPrimaryKey(companyInfo);
        }
        detailInfo.setCompanyInfoId(companyId);
        detailInfo.setName(companyInfo.getName());
        if (accountArr != null && accountArr != "") {
            detailInfo.setElecAccountNum(accountArr);
        }
        if (bankName != null && bankName != "") {
            detailInfo.setBankName(bankName);
        }
        if (bankAccount != null && bankAccount != "") {
            detailInfo.setBankAccount(bankAccount);
        }
        if (bankAccountName != null && bankAccountName != "") {
            detailInfo.setBankAccountName(bankAccountName);
        }
        if (voltageStep != null && voltageStep != "") {
            detailInfo.setVoltageStep(voltageStep);
        }
        if (transformerCapacity != null && transformerCapacity != "") {
            detailInfo.setTransformerCapacity(transformerCapacity);
        }
        if (elecConsumption != null && elecConsumption != "") {
            detailInfo.setElecConsumption(elecConsumption);
        }
        if (contactPhone != null && contactPhone != "") {
            detailInfo.setContactPhone(contactPhone);
        }
        if (detailCategory != null && detailCategory != "") {
            detailInfo.setDetailCategory(detailCategory);
        }
        if (fax != null && fax != "") {
            detailInfo.setFax(fax);
        }
        detailInfo.setUpdateAt(new Date());
        detailInfoMapper.updateByPrimaryKey(detailInfo);
        return detailInfo;
    }
}
